package com.aaa.marketresearch.android.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.Offer;
import com.aaa.marketresearch.android.service.HttpClient;
import com.aaa.marketresearch.android.util.SimpleLog;
import com.aaa.marketresearch.android.util.connector.IDataTextResponse;

/**
 * @author Nail
 */
public class OfferEditScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offer_edit_screen);
    }

    @Override
    protected void onResume() {
        super.onResume();

        final Offer offer = UserInfo.getInstance().getCurrentBillContent().getOffer();

        if (offer == null) {
            this.finish();
        }

        EditText barcodeEditText = (EditText) findViewById(R.id.offerBarcodeField);
        barcodeEditText.setText(offer.getBarcode().toString());

        EditText nameEditText = (EditText) findViewById(R.id.offerNameField);
        nameEditText.setText(offer.getName());

        EditText descriptionEditText = (EditText) findViewById(R.id.offerDescriptionField);
        descriptionEditText.setText(offer.getDescription());

        Spinner spinnerUnit = (Spinner) findViewById(R.id.offerUnitField);
        ArrayAdapter<CharSequence> adapterGender = ArrayAdapter.createFromResource(this, R.array.unit_Array, android.R.layout.simple_spinner_item);
        adapterGender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerUnit.setAdapter(adapterGender);

        int spinnerGenderPos = adapterGender.getPosition(offer.getUnits());
        spinnerUnit.setSelection(spinnerGenderPos);

        final LinearLayout offerUnitArea = (LinearLayout) findViewById(R.id.offerUnitArea);

        CheckBox countableCheckBox = (CheckBox) findViewById(R.id.offerCountableField);
        if (!offer.getCountable()) {
            countableCheckBox.setChecked(true);
            offerUnitArea.setVisibility(View.VISIBLE);
        } else {
            offerUnitArea.setVisibility(View.INVISIBLE);
            countableCheckBox.setChecked(false);
        }

        countableCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    offerUnitArea.setVisibility(View.VISIBLE);
                } else {
                    offerUnitArea.setVisibility(View.INVISIBLE);
                }
            }
        });
    }


    public void saveButtonClick(View view) {
        SimpleLog.info("saveButtonClick");

        final Offer offer = UserInfo.getInstance().getCurrentBillContent().getOffer();

        String barcode = ((EditText) findViewById(R.id.offerBarcodeField)).getText().toString();
        Long barcodeLong = 0L;
        try {
            barcodeLong = Long.parseLong(barcode);
        } catch (NumberFormatException ex) {
            SimpleLog.error(ex.toString());
        }

        offer.setBarcode(barcodeLong);

        String name = ((EditText) findViewById(R.id.offerNameField)).getText().toString();
        offer.setName(name);

        String description = ((EditText) findViewById(R.id.offerDescriptionField)).getText().toString();
        offer.setDescription(description);

        CheckBox countableCheckBox = (CheckBox) findViewById(R.id.offerCountableField);
        if (countableCheckBox.isChecked()) {
            offer.setCountable(false);
        } else {
            offer.setCountable(true);
        }
        Spinner spinnerUnit = (Spinner) findViewById(R.id.offerUnitField);

        String unit = spinnerUnit.getSelectedItem().toString();
        offer.setUnits(unit);

        HttpClient httpClient = new HttpClient(this.getApplicationContext());
        httpClient.saveOffer(offer, new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                SimpleLog.info("saved");
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(OfferEditScreen.this);
                alertDialog.setMessage("Сохранен");
                alertDialog.setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                OfferEditScreen.this.finish();
                            }
                        }
                );
                alertDialog.show();
            }

            @Override
            public void onError() {
                SimpleLog.info("error");
            }
        });
    }
}